package com.tuanzhang.mall.seller.domain.valueobj;

import cn.hutool.core.util.StrUtil;
import com.tuanzhang.mall.seller.types.exception.ApiException;
import lombok.Getter;

/**
 * 类功能描述
 *
 * @author wangyl
 * @date 2025/6/16
 */
@Getter
public class Email {


    private String email;

    private Email(String email) {
        if (StrUtil.isBlank(email)) {
            throw new ApiException("邮箱不能为空");
        } else if (!validateEmail(email)) {
            throw new ApiException("手机号校验不通过");
        }
        this.email = email;
    }

    private boolean validateEmail(String email) {
        // 检查电子邮件地址是否为空或只包含空白字符
        if (email == null || email.trim().isEmpty()) {
            return false;
        }

        // 正则表达式用于验证电子邮件地址的格式
        String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";

        // 使用正则表达式验证电子邮件地址
        return email.matches(emailRegex);
    }

    public static Email create(String email) {
        return new Email(email);
    }
}
